﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace frag_boardgame
{
    [DataContract]
    public class PlayerConfiguration
    {

        private Player.Controller control;

        [DataMember]
        public Player.Controller Control
        {
            get { return control; }
            set { control = value; }
        }


        string name;

        [DataMember]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        int maxHealth;

        [DataMember]
        public int MaxHealth
        {
            get { return maxHealth; }
            internal set {
                if (value < 1 || value > 4)
                    throw new ArgumentException("Max health must be between 1 and 4");
                maxHealth = value;
            }
        }

        int startingSpeed;

        [DataMember]
        public int StartingSpeed
        {
            get { return startingSpeed; }
            internal set
            {
                if (value < 1 || value > 4)
                    throw new ArgumentException("Starting speed must be between 1 and 4");
                startingSpeed = value;
            }
        }

        int startingAccuracy;

        [DataMember]
        public int StartingAccuracy
        {
            get { return startingAccuracy; }
            internal set
            {
                if (value < 1 || value > 4)
                    throw new ArgumentException("Starting accuracy must be between 1 and 4");
                startingAccuracy = value;
            }

        }

        Pos startingPosition;

        [DataMember]
        public Pos StartingPosition
        {
            get { return startingPosition; }
            set { startingPosition = value; }
        }

        public PlayerConfiguration()
        {
        }

        public void SetStats(int maxHealth, int startingSpeed, int startingAccuracy) 
        {
            if (maxHealth + startingSpeed + startingAccuracy > 7) {
                throw new ArgumentException("Sum of stats must not exceed 7");
            }

            MaxHealth = maxHealth;
            StartingSpeed = startingSpeed;
            StartingAccuracy = startingAccuracy;
        }

        /// <summary>
        /// Create a Human player (default)
        /// </summary>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <param name="name"></param>
        /// <param name="maxHealth"></param>
        public PlayerConfiguration(int row, int col, string name): this(row, col, name, Player.Controller.Human)
        {
           
        }

        /// <summary>
        /// Create a Player (AI or Human)
        /// </summary>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <param name="name"></param>
        /// <param name="maxHealth"></param>
        /// <param name="control"></param>
        public PlayerConfiguration(int row, int col, string name, Player.Controller control)
        {
            this.control = control;
            this.startingPosition = new Pos(row, col);
            this.name = name;

            // use default stats
            SetStats(3, 2, 2);
        }

        // override object.Equals
        public override bool Equals(object obj)
        {
            PlayerConfiguration other = obj as PlayerConfiguration;
            if (other == null) return false;

            return (name.Equals(other.name) &&
                maxHealth.Equals(other.maxHealth) &&
                startingAccuracy.Equals(other.startingAccuracy) &&
                startingSpeed.Equals(other.startingSpeed) &&
                startingPosition.Equals(other.startingPosition));
                
        }

        // override object.GetHashCode
        public override int GetHashCode()
        {
            return name.GetHashCode() ^ maxHealth.GetHashCode() ^ startingPosition.GetHashCode() ^ control.GetHashCode()
                ^ startingSpeed.GetHashCode() ^ startingAccuracy.GetHashCode();
        }
    }

}
